using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace SalesAutomationBL
{
    public class CQueryFormDetailsBL
    {
        StringBuilder sb = new StringBuilder();

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public string GetXMLData(String strParameterList, DataSet ds)
        {

            string operation = GetOperationValue(strParameterList);

            switch (operation)
            {
                case "AUTHENTICATE_USER":
                case "ACCOUNT_GETDETAILS":
                case "ACCOUNT_UPDATEDETAILS":
                case "CONTACT_GETDETAILS":
                case "CONTACT_UPDATEDETAILS":
                case "ACTIVITY_GETDETAILS":
                case "ACTIVITY_UPDATEDETAILS":
                case "TASK_GETDETAILS":
                case "TASK_UPDATEDETAILS":
                case "OPPORTUNITY_GETDETAILS":
                case "OPPORTUNITY_UPDATEDETAILS":
                case "OPP_PROD_GETDETAILS":
                case "OPP_PROD_UPDATEDETAILS":
                case "PARENTACCOUNT_GETDETAILS":
                case "PARENTACCOUNT_UPDATEDETAILS":
                case "RESELLERACCOUNT_GETDETAILS":
                case "RESELLERACCOUNT_UPDATEDETAILS":
                case "USERS_GETDETAILS":
                case "USERS_UPDATEDETAILS":
                case "PRODPRICE_GETDETAILS":
                case "PRODPRICE_UPDATE":

                    return ProcessResponse(ds);
                default:
                    break;
            }

            //Generate the xml string 
            sb.Append("<Data>");
            sb.Append((new CExceptionHandling.CCustomException()).XMLResponseCode);
            sb.Append("<Details>");
            AddColumnData(ds);
            sb.Append("</Details>");
            sb.Append("</Data>");

            // Return the XML created
            return sb.ToString();
        }

        private string GetOperationValue(string strParameterList)
        {
            string[] parameters = strParameterList.Split('&');

            foreach (string p in parameters)
            {
                if (p.ToUpper().StartsWith("OPERATION="))
                {
                    return p.Split('=')[1];
                }

            }
            return string.Empty;
        }


        private void AddColumnData(DataSet dsTemp)
        {
            DataRow dr = dsTemp.Tables[0].Rows[0];

            foreach (DataColumn dc in dsTemp.Tables[0].Columns)
            {
                sb.Append("<" + dc.ColumnName + ">");
                sb.Append(CheckForMasterData(dc.ColumnName, dr[dc.ColumnName].ToString()));
                sb.Append("</" + dc.ColumnName + ">");
            }

        }

        private string CheckForMasterData(string strColumnName, string strColumnValue)
        {
            CMasterDataBL objMaster = new CMasterDataBL();
            switch (strColumnName.ToUpper())
            {
                case "TR_ACCTGROUP":
                case "TR_ACTIVITYTYPE":
                case "TR_ADDRESSTYPE":
                case "TR_DATAORIGIN":
                case "TR_NOTETYPE":
                case "TR_OPPREFERRAL":
                case "TR_OPPSTAGE":
                case "TR_OPPTYPE":
                case "TR_PARTNERCATEGORY":
                case "TR_PARTNERDIVISION":
                case "TR_PARTNERTYPE":
                case "TR_RELATIONTYPE":
                case "STATE":
                    return GetDropDownXML(objMaster.GetStatesData(), strColumnValue);
                case "TR_VERTICAL":
                    return null;
                default:
                    return strColumnValue;
            }
        }

        private string GetDropDownXML(DataSet dsTemp, string strSelectedValue)
        {
            try
            {
                StringBuilder sbTemp = new StringBuilder();

                foreach (DataRow dr in dsTemp.Tables[0].Rows)
                {
                    sbTemp.Append("<item value=\"");
                    sbTemp.Append(dr[0].ToString());
                    sbTemp.Append("\" selected=\"");
                    if (dr[0].ToString() == strSelectedValue)
                        sbTemp.Append("true");
                    else
                        sbTemp.Append("false");
                    sbTemp.Append("\">");
                    sbTemp.Append(dr[1].ToString());
                    sbTemp.Append("</item>");
                }

                return sbTemp.ToString();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }



        #region Process Response
        private string ProcessResponse(DataSet ds)
        {
            return ds.Tables[0].Rows[0][0].ToString();
        }
        #endregion

    }
}
